<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">

      <el-checkbox v-for="arr in list" :key="arr.id" :label="arr.id"> {{ arr.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'// 为用户分配角色
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],

      list: []// 复选框数据
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    closeDialog() { // 取消
      this.$emit('close-dialog')
    },
    async loadRoles() { // 获取复选框数据
      const res = await getRoles({ page: 1, pagesize: 100 })// 没有接口，所以默认先显示1页的数据
      this.list = res.data.rows// 获取信息

      const info = await getUserDetailById(this.id)
      // 设置用户已有的权限
      this.roleIds = info.data.roleIds

      console.log('打印数据', this.roleIds)

      console.log(res)
    },
    async hSubmit() { // 保存当前用户信息
      const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
      console.log(res)
      this.$emit('close')
    }

  }
}
</script>
